/*
 Copyright 2007 Ole Rogeberg

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package org.lifeyears.web.action;

import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.lifeyears.model.User;
import org.lifeyears.services.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

/**
 * Generic action class
 *
 * @author Aleksander Blomskøld
 */
public abstract class AbstractLifeyearsAction
    extends ActionSupport
    implements SessionAware, Preparable, RequestAware
{

    public static final String USER_ID_SESSION_KEY = "userId";

    public static final String USER_REQUEST_KEY = "loggedInUser";

    protected final transient Logger log = LoggerFactory.getLogger( getClass() );

    protected Map<String, Object> session;

    protected Map<String, Object> request;

    private User user;

    // fields for buttons so we can see which button was pressed
    protected String cancel = null;

    protected String delete = null;

    protected String save = null;

    @Autowired
    protected UserService userService;

    @Override
    public void setSession( final Map session )
    {
        this.session = session;

        // get userId from session
        final Long userId = (Long) session.get( USER_ID_SESSION_KEY );

        // set user-field if userId was in session
        if ( userId != null )
        {
            user = userService.get( userId );
        }

    }

    public Map<String, Object> getSession()
    {
        return session;
    }

    public User getLoggedInUser()
    {
        return user;
    }

    public void setCancel( final String cancel )
    {
        this.cancel = cancel;
    }

    public void setDelete( final String delete )
    {
        this.delete = delete;
    }

    public void setSave( final String save )
    {
        this.save = save;
    }

    public void setUserService( final UserService userService )
    {
        this.userService = userService;
    }

    protected void logout()
    {
        user = null;
        session.remove( USER_ID_SESSION_KEY );
    }

    @Override
    public void setRequest( final Map request )
    {
        this.request = request;
    }

    public void doPrepare()
        throws Exception
    {
        // for sub-classes to implement
    }

    @Override
    public final void prepare()
        throws Exception
    {
        if ( user != null && request != null )
        {
            request.put( USER_REQUEST_KEY, user );
        }

        doPrepare();
    }

}
